package cn.backflow.admin.controller;

import cn.backflow.admin.common.Configuration;
import cn.backflow.admin.common.Constants;
import cn.backflow.admin.controller.base.BaseSpringController;
import cn.backflow.admin.entity.User;
import cn.backflow.lib.util.JsonMap;
import cn.backflow.lib.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class IndexController extends BaseSpringController {

    @Autowired
    private CacheManager cacheManager;

    @RequestMapping("/")
    public Object index(HttpServletRequest request) throws Exception {
        System.out.println("RequestMapping");
        /*
        List<Treeable> treeables = permissionService.findTree(SubjectUtil.getCurrentUser());
        request.setAttribute("menus", treeables);
        */
        return "index";
    }

    @RequestMapping("dashboard")
    public String dashboard(@RequestParam(value = "ver", required = false) String ver, HttpServletRequest request) {
        // request.setAttribute("totalRoomTopicDetail", statisticsService.totalRoomTopicDetail(null));
        return "dashboard" + StringUtil.value(ver);
    }


    @RequestMapping("logout")
    public Object logout(HttpSession session) throws Exception {
        session.removeAttribute(Constants.SESSION_USER_KEY);
        return JsonMap.succeed();
    }

    @RequestMapping(value = "unlock", method = RequestMethod.POST)
    public Object unlock(String password, HttpServletRequest request) {
        JsonMap json = JsonMap.fail(null);
        User user = getCurrentUser(request);
        if (user == null)
            json.put("expired", true);
        else if (!StringUtil.md5(password, user.getName()).equals(user.getPass()))
            json.put("msg", "密码不正确 !");
        else {
            request.getSession().removeAttribute(Constants.USER_SCREEN_LOCKED);
            json.put("success", true);
        }
        return json;
    }


    @RequestMapping("reload-config")
    public Object reloadConfig() {
        try {
            Configuration.reload();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return JsonMap.fail(e.getMessage());
        }
        return JsonMap.succeed().msg("系统配置重载成功!");
    }


    @ResponseBody
    @RequestMapping("clean-cache")
    public Object cleanCache(@RequestParam(value = "name", required = false, defaultValue = Constants.SYS_CACHE) String name) {

        for (String cacheName : cacheManager.getCacheNames()) {

            Cache cache = cacheManager.getCache(cacheName);

            if (name != null) {
                if (cacheName.equalsIgnoreCase(name)) {
                    cache.clear();
                    break;
                }
            } else {
                cache.clear();
            }
        }
        return JsonMap.succeed().msg("系统缓存清理成功!");
    }
}